﻿package pl.krasi 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.LocalConnection;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import pl.krasi.facebook.Model;
	import pl.krasi.game.Display;
	import pl.krasi.game.Snake;
	import flash.net.*;
	
	import gs.*;
	import gs.easing.*;
	
	import nl.demonsters.debugger.MonsterDebugger;
			
	
	/**
	 * ...
	 * @author Mblaszczyk
	 */
	public class fos extends Sprite
	{
	
		private var _md:Model;
		private var _ds:Display;
		private var _sn:Snake;
		public function fos() 
		{
			trace ("FB hello world");
			
			
			
			_md = new Model(this.loaderInfo);
			_md.addEventListener(Event.COMPLETE, onDataReady, false, 0, true);
			
			button.alpha = 0;
			button.buttonMode = true;
			button.addEventListener(MouseEvent.ROLL_OVER, overButton);
			button.addEventListener(MouseEvent.ROLL_OUT, outButton);
		}
		
		private function overButton(e:MouseEvent) : void {
			button.bar.x = button.bar.width;
			TweenLite.to(button.bar, 0.2, { x:0, ease:Quad.easeIn } );
		}
		
		private function outButton(e:MouseEvent) : void {
			TweenLite.to (button.bar, 0.2 , { x: -button.bar.width, ease:Quad.easeOut } );
		}
		
		
		private function onDataReady(e:Event) : void {
			
			TweenLite.to(button, 0.4, { alpha:1, ease:Quad.easeOut });
			button.addEventListener(MouseEvent.MOUSE_UP, onStart);
			

		}
		
		private function onStart(e:MouseEvent) : void {
			_ds = new Display(_md.pictureData);
			//TweenMax.to(_ds, 0, {colorMatrixFilter:{saturation:0}});
			addChild(_ds);
			
			/*var cOverlay:Sprite = new Sprite();
			cOverlay.name = "cOver";
			cOverlay.graphics.beginFill(0x55afad);
			cOverlay.graphics.drawRect(0, 0, 750, 480);
			cOverlay.graphics.endFill();
			cOverlay.blendMode = "overlay"
			addChild(cOverlay);*/
			
			
			_ds.alpha  = 0.2;
			_ds.addEventListener(Event.COMPLETE, onDisplayComplete);
			swapChildren(_ds, button);
			TweenLite.to(button, 0.4, { alpha:0, ease:Quad.easeOut } );
			TweenLite.to(logo, 0.6, { alpha:0, ease:Quad.easeOut } );
			button.removeEventListener(MouseEvent.MOUSE_UP, onStart);
		}
		
		private function onDisplayComplete(e:Event) : void {
			trace ("start weza");
			_sn = new Snake(_ds, stage);
			_sn.addEventListener(Event.CHANGE, onGameChange);
			
		}
		
		private function onGameChange(e:Event) : void {
			button.label.text = "RESTART";
			_sn.removeEventListener(Event.CHANGE, onGameChange);
			TweenLite.to(button, 0.4, { alpha:1, ease:Quad.easeOut });
			button.addEventListener(MouseEvent.MOUSE_UP, reStart);
			
			/*var infos:Array = new Array();
			infos['href'] = "http://apps.facebook.com/friendsofsnake/";
			infos['name'] = "Friends of Snake";
			infos['caption'] = _md._name + " got score of " + _sn.score + ".\nCould you do better?";*/
			
			var story:Object = new Object();
			story['name'] = 'Play Friends of Snake'; 
			story['href'] = 'http://apps.facebook.com/friendsofsnake/';
			//Friends of Snake
			
			story['media'] = [ { 'type': 'image', 'src':  'http://fos.mariuszblaszczyk.pl/snakeIco.jpg', 'href': 'http://apps.facebook.com/friendsofsnake/' } ] ;
			
			story['caption'] = '{*actor*} played Friends of Snake with score '  + _sn.score.toString() + '\n\nCould you do better?' ;
			
			
			
			var params:Array = new Array();
			params[0] = ""
			//params[1] = { 'media': [ { 'type': 'image', 'src':  'http://fos.mariuszblaszczyk.pl/snakeIco.jpg', 'href': 'http://apps.facebook.com/friendsofsnake/' } ] };
			//params[1] = { 'caption': '{*actor*} played friend of snake with score '  + _sn.score.toString() + 'not that bad :-0' };
			params[1] = story;
			//params[2] = infos;
			
			try {
				trace(params[1]['media'])
				trace(params[1]['media']['src'])
			} catch (e:Error) {
				trace ("nie ma tablicy media");
			}
			trace(params[1]['caption'])
			
			
			callFBJS('Facebook.streamPublish', params);
		}
		
		function callFBJS(methodName:String, parameters:Array):void { 
			
			var connection:LocalConnection = new LocalConnection(); 
			var connectionName:String =  this.loaderInfo.parameters.fb_local_connection; 
			
			if (connectionName) { 
				connection.send(connectionName, "callFBJS", methodName, parameters); 
			} 
			
		}
		
		private function reStart(e:MouseEvent) : void {
			TweenLite.to(button, 0.4, { alpha:0, ease:Quad.easeOut });
			_sn = null;
			_sn = new Snake(_ds, stage);
			_sn.addEventListener(Event.CHANGE, onGameChange);
		}
		
		function test () : void {
			/**
			 * TODO:
			 * 
			 * get friend list
			 * get friend icons
			 * build scene
			 * integrate with current game
			 * 
			 */
		}
		
	}
	
}